<template>
  <div class="app-container">
    <!--详情编辑-->
    <WarehouseMoveDetailEdit ref="openWarehouseMoveDetailEdit" @refresh="refresh"></WarehouseMoveDetailEdit>
    <div class="detail-title-box">
      <span>详情信息</span>
    </div>
    <!-- 货物调仓详情 -->
    <el-table
      :data="warehouseMoveDetails"
      show-summary
      :summary-method="getSummaries"
      v-loading="loading"
    >
    <af-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="left">
      <template slot-scope="scope">
        <el-button
          :disabled="getAuditState"
          size="mini"
          type="text"
          @click="handleDelete(scope.row)"
          v-hasPermi="['zhcloud:hwtc:del']"
        >删除</el-button>
        <el-button
          :disabled="getAuditState"
          size="mini"
          type="text"
          @click="handleInnerEdit(scope.row)"
          v-hasPermi="['zhcloud:hwtc:modify']"
        >库内修改</el-button>
      </template>
    </af-table-column>
      <af-table-column label="旧仓库条码" prop="sptmc" />
      <af-table-column label="新仓库条码" prop="sptmc2" />
      <af-table-column label="客户条码" prop="sptm" />
      <af-table-column label="调入件数" prop="spjs" />
      <af-table-column label="单重(KG)" prop="spjsl" />
      <af-table-column label="调入重量(吨)" prop="spsl" />
      <af-table-column label="调仓日期" prop="ssrqid" />
      <af-table-column label="原仓库" prop="spckmco" />
      <af-table-column label="原仓位" prop="ckcwo" />
      <af-table-column label="新仓库" prop="spckmcn" />
      <af-table-column label="新仓位" prop="ckcwn" />
      <af-table-column label="备注" prop="itbz"  width="150" :show-overflow-tooltip="true"/>
      <af-table-column label="物品规格" prop="spzs" />
      <af-table-column label="柜号" prop="spgh" />
      <af-table-column label="批号" prop="spbh" />
      <af-table-column label="物品名称" prop="spmc" />
      <af-table-column label="品牌" prop="spcd" />
      <af-table-column label="所有人" prop="spkhmc" width="250"/>
      <af-table-column label="纱支" prop="wpggA" />
      <af-table-column label="工艺" prop="wpggB" />
      <af-table-column label="工序" prop="wpggC" />
      <af-table-column label="用途" prop="wpggD" />
      <af-table-column label="产地" prop="wpggE" />
      <af-table-column label="漂染性" prop="wpggF" />
      <af-table-column label="生产日期" prop="wpggG" />
      <af-table-column label="颜色" prop="wpggH" />
    </el-table>
     <el-row type="flex" justify="center" :gutter="5" class="mt20">
      <el-col :span="1.5">
        <el-button type="primary" @click="handleWarehouseMoveReject" v-if="getAuditState" v-hasPermi="['zhcloud:hwtc:reject']">驳 回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" @click="warehouseMoveSubmit" v-if="!getAuditState" v-hasPermi="['zhcloud:hwtc:audit']">审 核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button @click="goBack">返回</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import WarehouseMoveDetailEdit from './warehouseMoveDetailEdit.vue'
import { getWarehouseMoveMoveDetails, warehouseMoveDelete,warehouseMoveReject, warehouseMoveAudit } from '@/api/storage/commonGoods/warehouseMove'
import { NUMBER_DIDGIT, WEIGHT_DIGIT } from '@/utils/constant'

export default {
  name: "warehouseMoveDetail",
  components: { WarehouseMoveDetailEdit },
  data() {
    return {
      //列表加载
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 详情信息
      detailsQuery: {
       	dzbz: "",
        dzid: "",
        dzzy10: "",
        dzzy6: "",
        dzzy7: "",
        dzzy8: "",
        iszxf: "",
        iszyl: "",
        wbdh: "",
        pzbh:"",
        khmc:""
      },
      //获取详情参数
      queryParams:{
        dzid:"",
      },
      //删除详情
      delQureyParams: {
        dzid: 0,
        isBa: "",
        khpb: 0,
        prrq: "",
        shopId: 0,
        spbhid: "",
        spbhid2: "",
        spckidis: [],
        spckidn: "",
        spckido: "",
        spcwidn: "",
        spcwido: "",
        spdzld: 0,
        spjsd: ""
      },
      //审核入参
      auditQureyParams:[],
      //是否审核
      auditState:"",
      //驳回入参
      rejectQureyParams:{
        dzid:"",
      },
      //行数据
      warehouseMoveDetails:[],
      //页数
      pageSize: 999,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const query = to.query
    next(vm => {   
      if(Object.keys(query).length > 0) {
        vm.reset(vm)
        vm.detailsQuery = query
        vm.queryParams.dzid =  query.dzid
        vm.auditState = query.shzt
        vm.delQureyParams.dzid = query.dzid
        vm.delQureyParams.prrq = query.prrq
        vm.rejectQureyParams.dzid = query.dzid
        vm.getList()
      }
    })
  },
  computed: {
    getAuditState() {
      return this.auditState  == "未审核"?false:true
    }
  },
  methods: {
    /**获取详情数据 */
    getList() {
      this.loading = true
      getWarehouseMoveMoveDetails(this.queryParams).then(response => {
        this.warehouseMoveDetails = response.data;
        this.loading = false
      })
    },
    /**删除详情 */
    deleteWarehouseMoveDetail() {
      this.$modal.loading("数据正在删除中，请稍等...")
      warehouseMoveDelete(this.delQureyParams).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('删除成功！')
        this.getList()
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },

    /**审核 */
    auditWarehouseMove() {
      this.$modal.confirm("是否确认次操作？").then(() => {
        this.$modal.loading("数据正在审核中，请稍等...")
        warehouseMoveAudit(this.auditQureyParams).then(response => {
          this.$modal.closeLoading()
          this.$modal.msgSuccess('审核通过！')
          this.goWarehouseMove()
        }).catch(()=> {
          this.$modal.closeLoading()
        })
      }).catch(() => {})
    },

    /**驳回 */
    rejectWarehouseMove() {
      this.$modal.confirm("是否确认次操作？").then(() => {
        this.$modal.loading("数据正在驳回中，请稍等...")
        warehouseMoveReject(this.rejectQureyParams).then(response => {
          this.$modal.closeLoading()
          this.$modal.msgSuccess('审核驳回！')
          this.goWarehouseMove()
        }).catch(()=> {
          this.$modal.closeLoading()
        })
      }).catch(() => {})
    },

    /**历史记录 */
    handleHistoryRecord() {
      this.$refs.openWarehouseMoveDetailHistory.open(this.detailsQuery)
    },
    /**删除行 */
    handleDelete(row) {
        this.$modal.confirm("是否删除？").then(() => {
        const {isBa= "",khpb= 0,shopId= 0,spbhid= "",spbhid2= "",spckidis= [],spckidn= "",spckido= "",spcwidn= "",spcwido= "",spdzld= 0,spjs:spjsd} = row
        this.delQureyParams = {...this.delQureyParams,isBa,khpb,shopId,spbhid,spbhid2,spckidis,spckidn,spckido,spcwidn,spcwido,spdzld,spjsd}
        this.deleteWarehouseMoveDetail()
      }).catch(() => {})
    },
    /**修改库内详情 */
    handleInnerEdit(row) {
        this.$refs.openWarehouseMoveDetailEdit.open(row, this.queryParams.dzid)
    },
    /**提交审核 */
    warehouseMoveSubmit() {
      if(this.auditState  == "已审核") return this.$modal.msgWarning("已审核的单不允许再次审核！")
      this.auditQureyParams.push(this.detailsQuery.dzid)
      this.auditWarehouseMove()
    },
    /** 驳回*/
    handleWarehouseMoveReject() {
      this.rejectWarehouseMove()
    },
    //合计
    getSummaries({ columns, data }) {
      const sums = []
      columns.forEach((column, index) => {
        if(index === 0) {
          sums[index] = "合计"
			    return
        }
        if ((column.property === "spjs" || column.property === "spsl")) {
          const values = data.map((item) => {
            return parseFloat(this.delcommafy(item[column.property]))
          });
          sums[index] = values.reduce((prev, curr) => {
            if(!prev) prev = 0
            if(!curr) curr = 0
            if(column.property === "spsl") {
              return parseFloat((prev + curr).toFixed(WEIGHT_DIGIT))
            }
             if(column.property === "spjs") {
              return parseFloat((prev + curr).toFixed(NUMBER_DIDGIT))
            }
          }, 0);
        }
      })
      return sums
    },
    //跳回货物调仓页
    goWarehouseMove() {
      this.$tab.closePage(this.$route)
      this.$router.push({name:"WarehouseMove", params:{refresh:true}})
    },
    reset(vm) {
      Object.assign(vm.$data, vm.$options.data())
    },
     /** 刷新数据*/
    refresh() {
      this.getList()
    },
      /**返回不刷新 */
    goBack() {
      this.$router.push({name:"WarehouseMove"})
    }
  }
};
</script>
